package com.monkey.luffer.springboot.demo.basis.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @Author: dengkaibing
 * @Description:
 * @Date: 2020/12/14 11:22
 * @company 甜橙金融
 * @Version: 1.0
 */
@Slf4j
public class JacksonTest {
    public static ObjectMapper mapper=new ObjectMapper();

    public static void main(String[] args) {
        User user=new User();
        user.setName("dkb");
        user.setAge(22);
        try {
            String jsonStr=mapper.writeValueAsString(user);
            log.info("jsonStr:{}",jsonStr);
            byte[] byteArray=mapper.writeValueAsBytes(user);
            log.info("byteAray:{}",byteArray);
            User user2=mapper.readValue(jsonStr, User.class);
            log.info("json to User:{}",user2);
        } catch (IOException e) {
            log.error("errors:",e);
        }

    }
    static class  User{
        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
        }
    }
}
